<!-- 编辑弹窗 -->
<template>
    <div class="ele-body">
      <el-card>
        <!-- 搜索表单 -->
        <recaccountDetailSearch @search="reload"  />
      </el-card>
      <el-card>
        <ele-pro-table ref="table"  row-key="id" :columns="warehouseItemColumns"  :datasource="datasource"  :need-page="true"  :cell-style="cellStyle" :height="tableHeight" :full-height="tableFullHeight">
          <!-- 单价 -->
          <template slot="amount" slot-scope="{ row }">
            ￥{{ row.amount }}
          </template>
          <template slot="exchangeAmount" slot-scope="{ row }">
            ￥{{ row.exchangeAmount }}
          </template>
          <template slot="billNo" slot-scope="{ row }"  >
            <el-link  type="primary"   @click="clickbillNo(row)" >{{ row.billNo }}</el-link>
          </template>
        </ele-pro-table>
      </el-card>
    </div>
  </template>
  <script>

  //初始化对像
  import { removePageTab, reloadPageTab,setPageTabTitle } from '@/utils/page-tab-util';
  import recaccountDetailSearch from '../components/recaccount-detail-search.vue';
  import { QueryRecaccountitemByCondition } from '@/api/finance/recaccountitem';

  export default {
    name: 'RecaccountEdit',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    components:{recaccountDetailSearch},
    data() {
      return {
        // 表单数据
        form: {},
        fixedHeight:true,
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: 3,
        customerId:'',
        currencyId:'',
        // 表格列配置
        warehouseItemColumns: [
          {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
          {prop: 'billNo', label: '单据编号', width: 120,showOverflowTooltip: true, slot:'billNo',sortable: 'billNo',align: 'center',},
          {prop: 'billTypeDesc', label: '单据类型',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'billTypeId'},
          {prop: 'directionDesc', label: '业务方向',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'direction'},
          {prop: 'customerName', label: '客户名称',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'customerId'},
          {
            prop: 'billDate', label: '单据日期', width: 120,showOverflowTooltip: true, align: 'center',sortable: 'billDate',
            formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
          },
          {
            prop: 'peddingDate', label: '过账日期', width: 120,showOverflowTooltip: true, align: 'center',sortable: 'peddingDate',
            formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
          },
          {prop: 'amount', label: '合计金额',width: 120, showOverflowTooltip: true,sortable:'amount', align: 'center',},
          // {prop: 'disAmount', label: '优惠金额',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'disAmount'},
          {prop: 'recAmount', label: '本单金额',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'recAmount'},
          {prop: 'recreceiptAmount', label: '收款金额',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'recreceiptAmount'},
          {prop: 'surplusAmount', label: '应收余额',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'surplusAmount'},
          {prop: 'currencyDesc', label: '币种',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'currencyId'},
          {prop: 'rate', label: '汇率', width: 150, showOverflowTooltip: true,sortable:'rate', align: 'center',},
          {prop: 'amountCn', label: '金额(￥)',width: 120, showOverflowTooltip: true,sortable:'amountCn', align: 'center',},
          // {prop: 'disAmountCn', label: '优惠金额(RMB)',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'disAmountCn'},
          {prop: 'recAmountCn', label: '本单金额(￥)',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'recAmountCn'},
          {prop: 'recreceiptAmountCn', label: '收款金额(￥)',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'recreceiptAmountCn'},
          {prop: 'surplusAmountCn', label: '应收余额(￥)',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'surplusAmountCn'},
        ],
      };
    },
    computed: {
      // 表格高度
      tableHeight() {
        var fh = this.fixedHeight ? 'calc(100vh - 350px)' : void 0;
        return fh;
      },
      // 表格全屏时高度
      tableFullHeight() {
        var fh =  this.fixedHeight ? 'calc(100vh - 250px)' : null;
        return fh;
      },
    },
    created(){
      this.customerId = this.$route.query.customerId;
      this.currencyId = this.$route.query.currencyId;
    },
    methods: {

      /* 表格数据源 */
      async datasource(params) {

        params.where.pageindex = params.page;
        params.where.pagesize = params.limit;

        //添加排序
        if(params.order.order == null || params.order.order == undefined){
          params.where.orderBys = [{ sort: "createDate", order: 1, }];
        }else{
          params.where.orderBys = [];
          params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
        }

        if(!params.where.conditions)params.where.conditions = {};

        if(this.customerId){
          params.where.conditions.customerId =  this.customerId;
        }

        if(this.form.currencyId){
          params.where.conditions.currencyId =  this.currencyId;
        }

        let data = await QueryRecaccountitemByCondition(params.where);


        this.$nextTick(() => {
          this.$refs.table.doLayout();
        });

        return {
            list:data.items,
            count:data.totalrecords,
        };
      },

      /* 刷新表格 */
      reload(where) {
        if(this.$refs.table){
          this.$refs.table.reload({ where: where });
        }
      },

      //单击单号
      clickbillNo(row){

        // 出库单
        if(row.billTypeId == 1){
          
          removePageTab({ key: '/businesshandling/saleoutmanger' });
          this.$nextTick(() => {
            this.$router.push({path: '/businesshandling/saleoutmanger', query: { "orderNo": row.billNo } });
          });
        }

         // 收款单
        if(row.billTypeId == 3){
          removePageTab({ key: '/financialmanager/recreceiptmanager' });
          this.$nextTick(() => {
            this.$router.push({path: '/financialmanager/recreceiptmanager', query: { "receiptNo": row.billNo } });
          });
        }
      },
      // /* 合计 */
      // getSummaries(param){
      //   const sums = [];
      //   sums[0] = '合计' ;
      //   const { columns, data } = param;
      //   columns.forEach((column, index) => {
      //     if(index ==9){
      //       sums[index] =  Number(this.qtyTotal).toFixed(2);
      //     }else if(index ==11){
      //       sums[index] = '￥'+Number(this.amountTotal).toFixed(2);
      //     }
      //   });

      //   this.$nextTick(() => {
      //       this.$refs.table.doLayout();
      //     });
      //   return sums;
      // },
  
      /* 样式 */
      cellStyle(param){
        const { row, column, rowIndex, columnIndex } = param;
        if(columnIndex==7||columnIndex==8||columnIndex==9||columnIndex==10||columnIndex==12||columnIndex==13||columnIndex==14||columnIndex==15||columnIndex==16){
          return{
            color:''
          }
        }
      },
  },
    
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          //赋值this.form
          this.form = this.data;
          this.isUpdate = true;

          this.reload();
        } 
      }
    }
  };
  </script>